package wt.hpad.base;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import wt.hpad.MainApp;


/**
 * RecyclerView 数据适配器基类
 * Created by wt on 17-8-17.
 */

public abstract class BaseRvAdapter<T> extends RecyclerView.Adapter {
    //    private final String TAG = "BaseRvAdapter";
    public static final int TYPE_HEAD = 0;
    public static final int TYPE_NORMAL = 1;
    public Activity activity;

    private List<T> dataSet;

    private Context mContext = MainApp.getContext();
    private View mHeadView = null;


    /**
     * 用构造函数给数据源赋值
     */
    public BaseRvAdapter(List<T> dataSet, Activity activity) {
        this.dataSet = dataSet;
        this.activity = activity;
    }

    /**
     * 设置"头"
     */
    public View createHeadView(Context context, ViewGroup parent, int layoutID) {
        mHeadView = LayoutInflater.from(context).inflate(layoutID, parent, false);
        return mHeadView;
    }

    protected abstract BaseHolder createViewHolder(Context context, ViewGroup parent, int viewType);

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return createViewHolder(mContext, parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEAD) {
            return;
        }
        int itemIndex = getItemIndex(position);
        T bean = dataSet.get(itemIndex);
        // holder 中自带 binding 功能,
        BaseHolder<T> itemholder = (BaseHolder<T>) holder;
        itemholder.bindItemValue(bean);
    }

    public List<T> getDataSet() {
        return dataSet;
    }

    private int getItemIndex(int position) {
        return mHeadView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeadView == null ? dataSet.size() : dataSet.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeadView == null) {
            return TYPE_NORMAL;
        } else if (position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_NORMAL;
        }
    }

    public View getHeadView() {
        return mHeadView;
    }

    /**
     * 从既有数据集来刷新列表
     */
    public void justRefresh() {
        notifyDataSetChanged();
    }

    /**
     * 增加 set of data
     */
    public void addDatas(List<T> datas) {
        int positionStart = getItemCount();
        dataSet.addAll(datas);
        notifyItemRangeChanged(positionStart, datas.size());
    }

    /**
     * 增加单条数据
     */
    public void addData(T data) {
        int position = getItemCount();
        dataSet.add(data);
        notifyItemChanged(position);
    }

    /**
     * @param position 就是 item 对应 Holder 在 layout 中的真实位置.
     */
    public void removeData(int position) {
        int index = getItemIndex(position);
        dataSet.remove(position);
        notifyItemRemoved(index);
    }

    /**
     * 修改某条数据的值
     */
    public void changeItemData(int position, T data) {
        int index = getItemIndex(position);
        dataSet.set(position, data);
        notifyItemChanged(index);
    }

    // 适配GridLayout 时,修改HeadView 的跨行(HeadView 占2行,NormalView 占1行)
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = (GridLayoutManager) layoutManager;
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == TYPE_HEAD ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

}
